//
//  ZYLSearchBaseViewController.m
//  Cartoon
//
//  Created by dllo on 15/7/14.
//  Copyright (c) 2015年 dllo. All rights reserved.
//

#import "ZYLSearchBaseViewController.h"
#import "Define.h"
#import "ZYLSearchTableView.h"
#import "ZYLSearchResultModel.h"
#import "AFNetworkHandler.h"
#import "ZYLSearchTypeCollectionView.h"
@interface ZYLSearchBaseViewController ()<UISearchBarDelegate>
@property (nonatomic, retain) UITextField *searchTopTextField;
@property (nonatomic, retain) UIButton *searchTopCancelButton;
@property (nonatomic, retain) ZYLSearchTableView *searchResultTableView;
@property (nonatomic, retain) NSArray *typeCollectionViewTitleArr;
@property (nonatomic, retain) ZYLSearchTypeCollectionView *typeCollectionView;
@end

@implementation ZYLSearchBaseViewController

- (void)dealloc
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    [_searchResultTableView release];
    [_searchTopTextField release];
    [_searchTopCancelButton release];
    [_typeCollectionView release];
    [_typeCollectionViewTitleArr release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    [self createTextFieldAndButton];
    [self createSearchResultTableView];
    [self getTypeCollectionViewData];
}

- (void)createTextFieldAndButton
{
    
    UIImageView *img = [[[UIImageView alloc] initWithFrame:CGRectMake(30, 30, 20, 20)] autorelease];
    img.image = [UIImage imageNamed:@"iconfont-search"];
    [self.view addSubview:img];
    
    
    self.searchTopTextField = [[[UITextField alloc] initWithFrame:CGRectMake(60, 27, SCREEN_WIDTH - 130, 64 - 39)] autorelease];
    self.searchTopTextField.placeholder = @"搜索作品名、作者名";
    [self.view addSubview:self.searchTopTextField];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(textFieldChanged:) name:UITextFieldTextDidChangeNotification object:self.searchTopTextField];
    
    UIView *v = [[[UIView alloc] initWithFrame:CGRectMake(0, 63.5, SCREEN_WIDTH, 0.5)] autorelease];
    v.backgroundColor = [UIColor lightGrayColor];
    [self.view addSubview:v];
    
    self.searchTopCancelButton = [UIButton buttonWithType:UIButtonTypeSystem];
    self.searchTopCancelButton.frame = CGRectMake(self.searchTopTextField.frame.origin.x + self.searchTopTextField.frame.size.width, 23, 50, 30);
    self.searchTopCancelButton.titleLabel.font = [UIFont systemFontOfSize:17];
    [self.view addSubview:self.searchTopCancelButton];
    [self.searchTopCancelButton setTitle:@"取消" forState:UIControlStateNormal];
    self.searchTopCancelButton.tintColor = [UIColor orangeColor];
    [self.searchTopCancelButton addTarget:self action:@selector(searchTopCancelButtonAction) forControlEvents:UIControlEventTouchUpInside];
}


- (void)textFieldChanged:(NSNotification *)notification
{
    UITextField *tf = notification.object;
    if ([tf.text isEqualToString:@""]) {
        self.searchResultTableView.hidden = YES;
        self.typeCollectionView.hidden = NO;
    } else {
        self.searchResultTableView.hidden = NO;
        self.typeCollectionView.hidden = YES;
        
        [ZYLSearchResultModel SearchResultWithURLKeyword:tf.text block:^(NSMutableArray *array) {
            self.searchResultTableView.searchResultDataArray = array;
            [self.searchResultTableView reloadData];
        }];
    }
}


- (void)getTypeCollectionViewData
{
    [AFNetworkHandler GETwithAFNByURL:@"http://api.kuaikanmanhua.com/v1/tag_suggestion" completion:^(id result) {
        self.typeCollectionViewTitleArr = result[@"data"][@"suggestion"];
        [self createTypeCollectionView];
    }];
}


- (void)createTypeCollectionView
{
    self.typeCollectionView = [[[ZYLSearchTypeCollectionView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT / 2)] autorelease];
    self.typeCollectionView.typeLabelTextArr = self.typeCollectionViewTitleArr;
    [self.view addSubview:self.typeCollectionView];
}



- (void)searchTopCancelButtonAction
{
    if (![self.searchTopTextField.text isEqualToString:@""]) {
        self.searchTopTextField.text = @"";
        self.searchResultTableView.hidden = YES;
        self.typeCollectionView.hidden = NO;
    } else if ([self.searchTopTextField.text isEqualToString:@""]){
        [self.searchTopTextField resignFirstResponder];
        [self dismissViewControllerAnimated:YES completion:^{
        }];
    }
}

- (void) createSearchResultTableView
{
    self.searchResultTableView = [[[ZYLSearchTableView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64) style:UITableViewStylePlain] autorelease];
    [self.view addSubview:self.searchResultTableView];
    self.searchResultTableView.hidden = YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}



@end
